<!DOCTYPE html>
<!--
Copyright (c) 2015 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/perf_insights/mappers/trace_import_cost.html">
<link rel="import" href="/perf_insights/mre/mre_result.html">
<link rel="import" href="/tracing/core/test_utils.html">
<link rel="import" href="/tracing/extras/measure/measure.html">

<script>
'use strict';
tr.b.unittest.testSuite(function() {
  var test_utils = tr.c.TestUtils;
  var MeasureAsyncSlice = tr.e.measure.MeasureAsyncSlice;

  test('traceImportCostTest', function() {
    /**
     * The structure of this async slice group
     *
     * |___________________||_____|
     *               s1       s2
     *    |_| |_________|
     *   s1_s1   s1_s2
     *            |_|
     *          s1_s2_s1
     **/
    var m = test_utils.newModel(function(m) {
      var p1 = m.getOrCreateProcess(1);
      var t2 = p1.getOrCreateThread(2);

      var s1 = test_utils.newSliceEx({
        type: MeasureAsyncSlice,
        title: 'TraceImport:s1',
        start: 0, end: 20
      });
      s1.isTopLevel = true;
      t2.asyncSliceGroup.push(s1);
      var s1_s1 = test_utils.newSliceEx({
        type: MeasureAsyncSlice,
        title: 'TraceImport:s1_s1',
        start: 3, end: 5
      });
      s1_s1.isTopLevel = false;
      s1.subSlices.push(s1_s1);
      var s1_s2 = test_utils.newSliceEx({
        type: MeasureAsyncSlice,
        title: 'TraceImport:s1_s2',
        start: 7, end: 17
      });
      s1_s2.isTopLevel = false;
      s1.subSlices.push(s1_s2);
      var s1_s2_s1 = test_utils.newSliceEx({
        type: MeasureAsyncSlice,
        title: 'TraceImport:s1_s2_s1',
        start: 11, end: 13
      });
      s1_s2_s1.isTopLevel = false;
      s1_s2.subSlices.push(s1_s2_s1);

      var s2 = test_utils.newSliceEx({
        type: MeasureAsyncSlice,
        title: 'TraceImport:s2',
        start: 21, end: 27
      });
      s2.isTopLevel = true;
      t2.asyncSliceGroup.push(s2);
    });
    var result = new pi.mre.MreResult();
    pi.m.getTraceImportCostReport(result, m);

    assert.equal(tr.b.dictionaryLength(result.pairs), 1);
    var trace_import_cost_info = result.pairs.trace_import_cost_info;
    assert.equal(trace_import_cost_info.slices.length, 2);

    var slice1 = trace_import_cost_info.slices[0];
    assert.equal(slice1.title, 's1');
    assert.equal(slice1.start, 0);
    assert.equal(slice1.duration, 20);

    var subSlices = slice1.subSlices;
    assert.equal(subSlices.length, 2);

    var subSlice_1 = subSlices[0];
    assert.equal(subSlice_1.subSlices.length, 0);
    assert.equal(subSlice_1.title, 's1_s1');
    assert.equal(subSlice_1.start, 3);
    assert.equal(subSlice_1.duration, 2);

    var subSlice_2 = subSlices[1];
    assert.equal(subSlice_2.subSlices.length, 1);
    assert.equal(subSlice_2.title, 's1_s2');
    assert.equal(subSlice_2.start, 7);
    assert.equal(subSlice_2.duration, 10);

    var subSlice_2_1 = subSlice_2.subSlices[0];
    assert.equal(subSlice_2_1.subSlices.length, 0);
    assert.equal(subSlice_2_1.title, 's1_s2_s1');
    assert.equal(subSlice_2_1.start, 11);
    assert.equal(subSlice_2_1.duration, 2);

    var slice2 = trace_import_cost_info.slices[1];
    assert.equal(slice2.subSlices.length, 0);
    assert.equal(slice2.title, 's2');
    assert.equal(slice2.start, 21);
    assert.equal(slice2.duration, 6);
  });
});
</script>
